package question10;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String[] split = s.split(",");
        Node node = buildTree(split);
        System.out.println(validBST(node));
    }

    static boolean validBST(Node root) {
        if (root == null) {
            return true;
        }
        if ((root.left != null && root.val < root.left.val)
                || (root.right != null && root.val > root.right.val)) {
            return false;
        }
        return validBST(root.left) && validBST(root.right);
    }

    static Node buildTree(String[] arr) {
        int rootValue = Integer.parseInt(arr[0]);
        Node root = new Node(rootValue);
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        int index = 0;
        while (!queue.isEmpty() && index < arr.length) {
            Node curNode = queue.poll();
            Node left = buildNode(++index, arr);
            Node right = buildNode(++index, arr);
            curNode.left = left;
            curNode.right = right;
            if (left != null) {
                queue.offer(left);
            }
            if (right != null) {
                queue.offer(right);
            }
        }
        return root;
    }

    static Node buildNode(int index, String[] arr) {
        if (index >= arr.length || "null".equals(arr[index])) {
            return null;
        }
        return new Node(Integer.parseInt(arr[index]));
    }

    static class Node {
        Node left;
        Node right;
        int val;
        public Node(int vale) {
            this.val = vale;
        }
    }

}

